#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

bool isprime(int n)
{
	if (n == 1) return false;
	if (n > 1)
	{
		for (int i = 2; i <= sqrt(n); i++)
		{
			if (n % i == 0)
				return false;
		}
	}
	return true;
}

vector<int> enumerate_factors(int x)
{
	vector<int> cnt;
	for (int i = 1; i <= sqrt(x); i++)
	{
		if (x % i)
			continue;
		cnt.push_back(i);
		if (i * i < x)
			cnt.push_back(x / i);
	}
	return cnt;
}

void solve()
{
	int n;
	cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; i++) a[i] = i;
	for (int i = n / 2; i >= 2; i--)
	{
		swap(a[i], a[i * 2]);
	}
	for (int i = 2; i <= n; i++)
	{
		if (a[i] == i && !isprime(i))
		{
			vector<int> fac = enumerate_factors(i);
			for (int j = 0; j < fac.size(); j++)
			{
				if (fac[j] != 1 && fac[j] != i)
				{
					swap(a[find(a.begin(), a.end(), fac[j]) - a.begin()], a[i]);
					break;
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) cout << a[i] << ' ';
	cout << '\n';
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}